package com.example.k.littletag.component;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.example.k.littletag.R;

/**
 * Created by K on 2015/9/16.
 */
public class RevealView extends ImageView {


    private static final String TAG = "DxRevealLayout";
    private static final boolean DEBUG = true;

    //private static final double gapStandard = 20;

    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private int mTargetWidth;
    private int mTargetHeight;
    private int mMinBetweenWidthAndHeight;
    private int mMaxBetweenWidthAndHeight;
    private int mMaxRevealRadius;
    private int mRevealRadiusGap;
    private int mRevealRadius = 0;
    private float mCenterX;
    private float mCenterY;
    private int[] mLocationInScreen = new int[2];

    private boolean mShouldDoAnimation = false;
    private boolean mIsPressed = false;
    private int INVALIDATE_DURATION = 40;

    //private DispatchUpTouchEventRunnable mDispatchUpTouchEventRunnable = new DispatchUpTouchEventRunnable();

    public RevealView(Context context) {
        super(context);
        init();
    }

    public RevealView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RevealView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setWillNotDraw(false);
        mPaint.setColor(getResources().getColor(R.color.reveal_red));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        this.getLocationOnScreen(mLocationInScreen);
        Log.d("Location", mLocationInScreen[0] + "    " + mLocationInScreen[1] + "");
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);

        if (!mShouldDoAnimation || mTargetWidth <= 0) {
            return;
        }

        if (mRevealRadius > mMinBetweenWidthAndHeight / 2) {
            mRevealRadius += mRevealRadiusGap * 4;
        } else {
            mRevealRadius += mRevealRadiusGap;
        }
        this.getLocationOnScreen(mLocationInScreen);
        int[] location = new int[2];
        this.getLocationOnScreen(location);
        int left = location[0] - mLocationInScreen[0];
        int top = location[1] - mLocationInScreen[1];
        int right = left + this.getMeasuredWidth();
        int bottom = top + this.getMeasuredHeight();

        canvas.save();
        canvas.clipRect(left, top, right, bottom);
        canvas.drawCircle(mCenterX, mCenterY, mRevealRadius, mPaint);
        canvas.restore();

        if (mRevealRadius <= mMaxRevealRadius) {
            postInvalidateDelayed(INVALIDATE_DURATION, left, top, right, bottom);
        } else if (!mIsPressed) {
            mShouldDoAnimation = false;
            postInvalidateDelayed(INVALIDATE_DURATION, left, top, right, bottom);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        mCenterX = (int) event.getRawX();
        mCenterY = (int) event.getRawY();

        Log.d("Location", "mCenterX: "+mCenterX+"  mCenterY:"+mCenterY);

        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            Log.d("Location", "Action_down");

            initParametersForChild(this);
            postInvalidateDelayed(INVALIDATE_DURATION);
            this.setEnabled(false);
        } else if (action == MotionEvent.ACTION_UP) {

            Log.d("Location", "Action_up");
            mIsPressed = false;
            postInvalidateDelayed(INVALIDATE_DURATION);
            //mDispatchUpTouchEventRunnable.event = event;
            //postDelayed(mDispatchUpTouchEventRunnable, 40);
            return true;
        } else if (action == MotionEvent.ACTION_CANCEL) {
            mIsPressed = false;
            postInvalidateDelayed(INVALIDATE_DURATION);
        }

        return super.dispatchTouchEvent(event);
    }

    private void initParametersForChild(View view) {
        //mCenterX = this.getX() + this.getWidth()/2;
        //mCenterY = this.getY() + this.getHeight()/2;

        WindowManager wm = (WindowManager) getContext()
                .getSystemService(Context.WINDOW_SERVICE);
        mTargetHeight = wm.getDefaultDisplay().getHeight();
        mTargetWidth = wm.getDefaultDisplay().getWidth();


        Log.d("Location", "mTargetWidth = " + mTargetWidth + "  " + "mTargetHeight = " + mTargetHeight);

        mMinBetweenWidthAndHeight = Math.min(mTargetWidth, mTargetHeight);
        mMaxBetweenWidthAndHeight = Math.max(mTargetWidth, mTargetHeight);
        mRevealRadius = 0;
        mShouldDoAnimation = true;
        mIsPressed = true;
        mRevealRadiusGap = mMinBetweenWidthAndHeight / 10;

        //int[] location = new int[2];
        //view.getLocationOnScreen(location);
        //int left = location[0] - mLocationInScreen[0];
        //int transformedCenterX = (int)mCenterX - left;
        //mMaxRevealRadius = Math.max(transformedCenterX, mTargetWidth - transformedCenterX);
        mMaxRevealRadius = (int) Math.sqrt(Math.pow(mMinBetweenWidthAndHeight, 2) + Math.pow(mMaxBetweenWidthAndHeight, 2));
    }


    private boolean isTouchPointInView(View view, int x, int y) {
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        int left = location[0];
        int top = location[1];
        int right = left + view.getMeasuredWidth();
        int bottom = top + view.getMeasuredHeight();
        if (view.isClickable() && y >= top && y <= bottom
                && x >= left && x <= right) {
            return true;
        }
        return false;
    }

   /* @Override
    public boolean performClick() {
        postDelayed(this, 400);
        return true;
    }

    *//*@Override
    public void run() {
        super.performClick();
    }*//*

    private class DispatchUpTouchEventRunnable implements Runnable {
        public MotionEvent event;

        @Override
        public void run() {

            if (isTouchPointInView(this, (int)event.getRawX(), (int)event.getRawY())) {
                this.performClick();
            }
        }
    };*/
}
